import rRequest from "@/assets/axios/request.js";

export default {
    insert(root, payload) {
        return new Promise((resolve, reject) => {
            let headers = {};
            let formData;
            let method = "insert"
            if (payload.file) {
                headers = {
                    'Content-Type': 'multipart/form-data'
                }

                formData = new FormData();
                formData.append('file', payload.file);
                delete payload['file'];
                formData.append('data', JSON.stringify(payload));

                method = "insert1";
            } else {
                formData = payload;
            }
            rRequest.POST({
                url: "/" + root + "/" + method,
                data: formData,
                headers: headers
            }).then(res => {
                resolve(res);
            }).catch(e => {
                reject(e);
            })
        })
    },
    update(root, payload) {
        return new Promise((resolve, reject) => {
            let headers = {};
            let formData;
            let method = "update"
            if (payload.file) {
                headers = {
                    'Content-Type': 'multipart/form-data'
                }
                formData = new FormData();
                formData.append('file', payload.file);
                delete payload['file'];
                formData.append('data', JSON.stringify(payload));

                method = "update/info";
            } else {
                formData = payload;
            }
            rRequest.POST({
                url: "/" + root + "/" + method,
                data: formData,
                headers: headers
            }).then(res => {
                resolve(res);
            }).catch(e => {
                reject(e);
            })
        })
    },
    delete(root, id) {
        return new Promise((resolve, reject) => {
            rRequest.POST({
                url: "/" + root + "/delete",
                params: {id: id}
            }).then(res => {
                resolve(res);
            }).catch(e => {
                reject(e);
            })
        })
    },
    selectList(root, json) {
        return new Promise((resolve, reject) => {
            rRequest.POST({
                url: "/" + root + "/select/list",
                data: json
            }).then(res => {
                resolve(res);
            }).catch(e => {
                reject(e);
            })
        })
    },
    selectById(root, id) {
        return new Promise((resolve, reject) => {
            rRequest.POST({
                url: "/" + root + "/select/by/id",
                params: {id: id}
            }).then(res => {
                resolve(res);
            }).catch(e => {
                reject(e);
            })
        })
    },
    selectPage(root, json) {
        return new Promise((resolve, reject) => {
            rRequest.POST({
                url: "/" + root + "/select/by/page",
                data: json
            }).then(res => {
                resolve(res);
            }).catch(e => {
                reject(e);
            })
        })
    },
}
